package com.leijie.zerocopy;

import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.text.DecimalFormat;

/**
 * @author LeiJie
 * @time 2020-5-15 16:35
 * @desc 传统的soket io客户端
 */
public class OldIOClient {

    public static void main(String[] args) throws IOException {
        Socket socket = new Socket("localhost",8899);

        String filePath = "E:\\BaiduNetdiskDownload\\netty\\netty高并发-张龙\\50_零拷贝实例深度剖析 吾爱程序猿ww.52programer.com.mp4";
        InputStream inputStream = new FileInputStream(filePath);
        DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());

        int readCount = 0;
        int totalCount = 0;
        byte[] fileBuf = new byte[1024];

        long startTime = System.currentTimeMillis();

        while ((readCount = inputStream.read(fileBuf)) >=0){
            totalCount+=readCount;
            outputStream.write(fileBuf);
        }
        long endTime = System.currentTimeMillis();
        System.out.println("发送总字节数："+totalCount+">>>总耗时："+(endTime-startTime));
        outputStream.close();
        socket.close();
        inputStream.close();
    }

    public static String readableFileSize(long size) {
        if (size <= 0) return "0";
        final String[] units = new String[]{"B", "Kb", "Mb", "Gb", "Tb"};
        int digitGroups = (int) (Math.log10(size) / Math.log10(1024));
        return new DecimalFormat("#,##0.#").format(size / Math.pow(1024, digitGroups)) + "" + units[digitGroups]+"/s";
    }
}
